package com.leather.activity.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.leather.activity.vo.ActivityQueryVo;
import com.leather.activity.vo.PassedVo;
import com.leather.common.entity.Activity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
*  @author kevin
*/
public interface ActivityMapper extends BaseMapper<Activity> {


    @Select("select a.*,b.img_url,b.nick_name from activity a,user b where a.user_id = b.id and a.now_people<max_people and a.end_time > now() order by a.id desc limit #{pageId},20")
    List<ActivityQueryVo> index(@Param("pageId") int pageId);

    @Select("select a.*,b.img_url,b.nick_name from activity a,user b where a.user_id = b.id and a.id in" +
            " (select activity_id from activity_get where user_id = #{userId}) order by a.id desc")
    List<ActivityQueryVo>  userIndex(@Param("userId") int userId);


    @Select("select * from activity  where id = #{activityId} for update")
    Activity getActivityById(@Param("activityId") int activityId);

    @Delete("delete from activity where id = #{activityId} and user_id = #{userId} and now_people = 0")
    int deleteActivity(@Param("activityId") int activityId, @Param("userId") int userId);

    @Update("update activity set now_people = now_people+1 where id = #{activityId} and now_people < max_people")
    int updateActivity(@Param("activityId") int activityId);


    @Select("select a.real_name,a.phone from  user a,activity_get b where b.user_id = a.id and b.activity_id = #{activityId}")
    List<PassedVo> getPassedUser(@Param("activityId") int activityId);
}